package com.test.daily.leetcode.y2022.m07.day0720.v02;

import java.util.HashSet;
import java.util.Set;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/20 上午 11:57
 * @version: 1.0
 */
public class Solution {
    public static int minLight(String s) {
        Set<Integer> set = new HashSet<>();
        return process(s, set, 0);
    }

    public static int process(String s, Set<Integer> set, int index) {
        if (index == s.length()) {
            for (int i = 1; i < s.length(); i++) {
                if (!set.contains(i - 1) && !set.contains(i) && !set.contains(i + 1)) {
                    return Integer.MAX_VALUE;
                }
            }
            return set.size();
        } else {
            int no = process(s, set, index + 1);
            int yes = Integer.MAX_VALUE;
            if (s.charAt(index) == '.') {
                set.add(index);
                yes += process(s, set, index + 1);
                set.remove(index);
            }
            return Math.min(yes, no);
        }
    }

    public static int min2(String s) {
        int i = 0;
        int lights = 0;
        while (i < s.length()) {
            if (s.charAt(i) == 'X') {
                i++;
            } else {
                lights++;
                if (i + 1 == s.length()) {
                    break;
                } else {
                    if (s.charAt(i + 1) == 'X') {
                        i = i + 2;
                    } else {
                        i = i + 3;
                    }
                }
            }
        }
        return lights;
    }

    public static int min3(String s) {
        Set<Integer> set = new HashSet<>();
        return process(s, 0, set);
    }

    public static int process(String s, int index, Set<Integer> set) {
        if (index == s.length()) {
            for (int i = 1; i < s.length(); i++) {
                if (!set.contains(i - 1) && !set.contains(i) && !set.contains(i + 1)) {
                    return Integer.MAX_VALUE;
                }
            }
            return set.size();
        } else {
            int no = process(s, index + 1, set);
            int yes = Integer.MAX_VALUE;
            if (s.charAt(index) == '.') {
                set.add(index);
                yes = process(s, index + 1, set);
                set.remove(index);
            }
            return Math.min(yes, no);
        }
    }

    public static int min4(String s) {
        int lights = 0;
        int i = 0;
        while (i < s.length()) {
            if (s.charAt(i) == 'X') {
                i++;
            } else {
                lights++;
                if (i + 1 == s.length()) {
                    break;
                } else {
                    if (s.charAt(i + 1) == 'X') {
                        i = i + 2;
                    } else {
                        i = i + 3;
                    }
                }
            }
        }
        return lights;
    }

}
